<template>
  <Teleport to="body">
    <Transition name="modal-fade">
      <div v-if="visible" class="custom-message-box-overlay" @click="handleOverlayClick">
        <Transition name="modal-scale">
          <div v-if="visible" class="custom-message-box" @click.stop>
            <div class="custom-message-box__header">
              <div class="custom-message-box__title">{{ title }}</div>
            </div>
            
            <div class="custom-message-box__body">
              <div class="custom-message-box__icon" v-if="type">
                <component :is="iconComponent" />
              </div>
              <div class="custom-message-box__message">{{ message }}</div>
            </div>
            
            <div class="custom-message-box__footer">
              <button 
                class="custom-message-box__button custom-message-box__button--cancel"
                @click="handleCancel"
              >
                {{ cancelButtonText }}
              </button>
              <button 
                class="custom-message-box__button custom-message-box__button--confirm"
                @click="handleConfirm"
              >
                {{ confirmButtonText }}
              </button>
            </div>
          </div>
        </Transition>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import {
  Warning,
  InfoFilled,
  CircleClose,
  QuestionFilled
} from '@element-plus/icons-vue'

interface Props {
  message: string
  title?: string
  type?: 'warning' | 'info' | 'error' | 'question'
  confirmButtonText?: string
  cancelButtonText?: string
  onConfirm?: () => void
  onCancel?: () => void
}

const props = withDefaults(defineProps<Props>(), {
  title: '提示',
  type: 'warning',
  confirmButtonText: '确定',
  cancelButtonText: '取消'
})

const visible = ref(false)

const iconComponent = computed(() => {
  const iconMap = {
    warning: Warning,
    info: InfoFilled,
    error: CircleClose,
    question: QuestionFilled
  }
  return iconMap[props.type]
})

const handleConfirm = () => {
  visible.value = false
  setTimeout(() => {
    props.onConfirm?.()
  }, 300)
}

const handleCancel = () => {
  visible.value = false
  setTimeout(() => {
    props.onCancel?.()
  }, 300)
}

const handleOverlayClick = () => {
  handleCancel()
}

onMounted(() => {
  visible.value = true
})
</script>

<style scoped>
.custom-message-box-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-message-box {
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  min-width: 400px;
  max-width: 500px;
  overflow: hidden;
}

.custom-message-box__header {
  padding: 20px 20px 0;
}

.custom-message-box__title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.custom-message-box__body {
  padding: 20px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.custom-message-box__icon {
  font-size: 24px;
  flex-shrink: 0;
  margin-top: 2px;
}

.custom-message-box__icon.warning {
  color: #e6a23c;
}

.custom-message-box__icon.info {
  color: #409eff;
}

.custom-message-box__icon.error {
  color: #f56c6c;
}

.custom-message-box__icon.question {
  color: #409eff;
}

.custom-message-box__message {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
  flex: 1;
}

.custom-message-box__footer {
  padding: 0 20px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.custom-message-box__button {
  padding: 8px 16px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid;
  transition: all 0.2s;
  min-width: 80px;
}

.custom-message-box__button--cancel {
  background: white;
  border-color: #dcdfe6;
  color: #606266;
}

.custom-message-box__button--cancel:hover {
  border-color: #c0c4cc;
  color: #409eff;
}

.custom-message-box__button--confirm {
  background: #409eff;
  border-color: #409eff;
  color: white;
}

.custom-message-box__button--confirm:hover {
  background: #66b1ff;
  border-color: #66b1ff;
}

.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-scale-enter-active,
.modal-scale-leave-active {
  transition: all 0.3s ease;
}

.modal-scale-enter-from,
.modal-scale-leave-to {
  opacity: 0;
  transform: scale(0.8);
}
</style>
